package rule

import "regexp"

type emailRule struct {
	reg *regexp.Regexp
}

func init() {
	Register(&emailRule{reg: regexp.MustCompile(`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`)})
}

func (e *emailRule) Name() string {
	return "email"
}

func (e *emailRule) Check(in *RuleInput) (err error) {
	if v, ok := in.Value.(string); ok && e.reg.FindStringIndex(v) != nil {
		return
	}
	return in.Error()
}
